﻿using System;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_nested_parent_toObject_is_null
    {
        private readonly Mapping<A, Aa> mapping;
        private readonly A fromObject;

        public class A { public B B { get; set; } }
        public class B {public String Text { get; set; }}

        public class Aa { public Bb B { get; set; } }
        public class Bb { public String Text { get; set; } }
        
        public When_nested_parent_toObject_is_null()
        {
            mapping = new Mapping<A, Aa>();
            mapping.Relate(x => x.B.Text, y => y.B.Text);

            fromObject = new A { B = new B { Text = "aText" } };
        }

        [Fact]
        public void Should_try_to_create_when_using_automagically_mapping()
        {
            
            var toObject = mapping.Map(fromObject);
            Assert.Equal("aText", toObject.B.Text);
        }

        [Fact]
        public void Should_throw_useful_exception_when_using_strict_mapping()
        {
            Assert.Throws(typeof(GlueException), ()=> mapping.Map(fromObject, new Aa()));
        }
    }
}
